var passwdCallbackCtrl  = new XFApp.client();

XFApp.client.prototype.passwdCallback = {
		successCode : 10000,
		sendSuccessCode: 40023,
		exsistCode:40002,
		completeSuccessCode:40025,
		urlItems : {	
			step1SaveURL: basePath+"/register/backStep1",
			verifyCodeSenderURL: basePath+"/register/verifyCodeSender",
			submitURL: basePath+"/register/backComplete",		
			verifyCodeURL : basePath+"/verifyCode/creator?timestamp=",
			verifyCodeCheckURL: basePath+"/verifyCode/check",
			isExistURL: basePath+"/register/isExist",
			loginURL: basePath+"/login/login",	
		},
		
		form1Validate : function(){
			$("#regFrom1").validate({				  
				errorPlacement : function(error, element) {
					if (element.is(":radio"))
						error.appendTo(element.parent().parent());
					else if (element.is(":checkbox"))
						error.appendTo(element.parent().parent());					
					else
						error.insertAfter(element); 
				},		        
		    });
			$("#regFrom1").validate();
		},
		
		form2Validate : function(){
			$("#regFrom2").validate({				  
				errorPlacement: function(error, element) {
					if(element.is(':radio') || element.is(':checkbox')){
						error.appendTo(element.parent());
					}else{
						error.insertAfter(element);  
					}					
				},  
		        rules: {
		        	email:{
	        	 		  required:true,
	        	 		  email:true
	         			  }
				  },
				 messages: {
					 email:{
		        	 		required:"请填写电子邮件",
		        	 		email:"电子邮件格式不正确"
		         			}
				  }
		    });
			$("#regFrom2").validate();
		},
		
		form3Validate : function(){
			$("#regFrom3").validate({
				errorPlacement: function(error, element) {    //错误提示在什么地方 
					if(element.is(':radio') || element.is(':checkbox')){
						error.appendTo(element.parent());
					}else{
						error.insertAfter(element);  
					}
					
				}, 
		        rules: {		        
		        	password:  {required:true,
		        			    minlength:6,
		        		        maxlength:20
		        			   },		        	
		        	password2: {
		        		        required:true,
		        		        equalTo: "#password"
		        	           }
				  },
				 messages: {					
			         password: {
			        	        required:"请填写新密码",
			        	        minlength: $.validator.format("请至少输入{0}个字符."),
			        	        maxlength: $.validator.format("请最多输入{0}个字符."),
			         			},
			         password2:{
			        	 		required:"请再次输入新密码",
			        	 		equalTo:"两次输入的密码不一致"
			         			}
				  }
		    });
			$("#regFrom3").validate();
		},
		
		loginFormValidate : function(){
			$("#loginForm2").validate({
				errorPlacement : function(error, element) {
					if (element.is(":radio"))
						error.appendTo(element.parent());
					else if (element.is(":checkbox"))
						error.appendTo(element.parent().parent());
					else if(element.is(":input"))
						error.appendTo(element.parent());
					else
						error.insertAfter(element); 
				},
		        rules: {
		        	userAccount2:   "required",
		        	userPassword2:  "required"
				  },
				 messages: {
					 userAccount2:   "请输入用户名",					           
					 userPassword2:  "请输入密码"
				  }
		    });
			$("#loginForm2").validate();
		},
		
		login : function (){			
			_this = this;			
			$("#loginBtn2").on("click",function(){				
				if($("#loginForm2").validate().form()){
					passwdCallbackCtrl.passwdCallback.loged();					
				}				
			});
			//表单验证
			_this.loginFormValidate();			
		},	
		
		
		loged : function (){
			
			var entity = {
		        userId : $("#userAccount2").val(),
				password : $("#userPassword2").val(),
			}
	    	
	    	$.ajax({
	    		url :_this.urlItems.loginURL,
	    		data : entity,
	    		dataType : "json",
	    		type : 'post',
	    		success : function(data) {				
	    			if(data.code==_this.successCode){ 
	    				window.location.href= basePath;
	    			}else{
	    				window.webDialogUtil.alert("提示",data.msg);
	    			}
	    		}
	    	});	
		
		},
		
		event : function (){			
			_this = this;
			
			
			$("#step1").on("click",function(){			
				
				if(!$("#userId").val()){
					$("#userId-error").html('请填写用户帐号');
				}
				
				if(!$("#fourcode").val()){
					$("#fourcode-error").html('请输入验证码');
				}				
				
				if($("#regFrom1").validate().form()){
					if(!$("#userId-error").text() && !$("#fourcode-error").text()){	
						passwdCallbackCtrl.passwdCallback.step1();		
					}
				}				
			});		
			
			$("#sendVerifyCode").on("click",function(){	
				
				if($("#regFrom2").validate().form()){
					passwdCallbackCtrl.passwdCallback.sender();					
				}				
			});
			
			
			$("#step2").on("click",function(){
				
				if(!$("#verifyCode").val()){
					$("#verifyCode-error").html('请输入邮箱验证码');
				}
				
				if($("#regFrom2").validate().form()){										
					if(!$("#verifyCode-error").text()){
						passwdCallbackCtrl.passwdCallback.step2();					
					}					
				}				
			});			
			
			$("#submitBtn").on("click",function(){				
				if($("#regFrom3").validate().form()){
					passwdCallbackCtrl.passwdCallback.submit();	
				}				
			});
			
			//表单验证
			_this.form1Validate();
			_this.form2Validate();
			_this.form3Validate();
			
			$("#userId").on("blur",function(){
				_this.checkAccount();
			});
			
			$("#fourcode").on("blur",function(){
				_this.verifyCodeCheck('fourcode');
			});	
			
			$("#verifyCode").on("blur",function(){
				_this.verifyCodeCheck('verifyCode');
			});			
			
			$("#next").on("click",function(){
				_this.nextImg();
			});
		},	
		
		nextImg : function(){
			var url = _this.urlItems.verifyCodeURL+(new Date()).valueOf();
			$("#verifyCodeImg").attr("src",url);
		},
		
		checkAccount : function(){
				var vo = {
				    userId : $("#userId").val()			
				}
				
				$.ajax({
		    		url :_this.urlItems.isExistURL,
		    		data : vo,
		    		dataType : "json",
		    		type : 'post',
		    		async:false,
		    		success : function(data) {				
		    			if(data.code !=_this.exsistCode){
		    				$("#userId-error").html('用户帐号不存在');
		    			}else{
		    				$("#userId-error").html("");
		    			}
		    		}
		    	});		
		},
		
		
		verifyCodeCheck : function(name){	
			var sessionKey ;
			if(name == 'fourcode')
				sessionKey = 'verifyCode' ;
			else
				sessionKey = 'callbackVerifyCode';
			
			var params = {
				verifyCode : $("#"+name).val(),
				sessionKey : sessionKey,
			}
			
			$.ajax({
	    		url :_this.urlItems.verifyCodeCheckURL,
	    		data : params,
	    		dataType : "json",
	    		type : 'post',
	    		async:false,
	    		success : function(data) {				
	    			if(data.code !=_this.successCode){ 
	    				$("#"+name+"-error").html(data.msg);
	    			}else{
	    				$("#"+name+"-error").html("");
	    			}
	    		}
	    	});
		},
		
		step1:function(){	
			
			var entity = {
				userId : $("#userId").val(),
			}
			
			$.ajax({
	    		url :_this.urlItems.step1SaveURL,
	    		data : entity,
	    		dataType : "json",
	    		type : 'post',
	    		success : function(data) {				
	    			if(data.code==_this.successCode){ 
	    				$('#reg1Div').addClass('fn-hide');
						$('#reg2Div').removeClass('fn-hide');
						$('#status #js_step1').removeClass('active');
						$('#status #js_step2').addClass('active');
						$("#hiddenId").val(data.result.id);
						$("#showUserId").html("您正在找回的帐号是："+data.result.userId);
						$("#successTip").html("恭喜，用户帐号 "+data.result.userId+" 重置密码成功");
	    			}else{
	    				window.webDialogUtil.alert("提示",data.msg);
	    			}
	    		}
	    	});			
		},
		
		sender:function(){	
			
			var params = {
				email:$("#email").val()
			}
			
			$.ajax({
	    		url :_this.urlItems.verifyCodeSenderURL,
	    		data : params,
	    		dataType : "json",
	    		type : 'post',
	    		success : function(data) {
	    			if(data.code == _this.sendSuccessCode){
	    				window.webDialogUtil.alert("提示",data.msg);
	    			}else{
	    				window.webDialogUtil.alert("提示",data.msg);
	    				$("#email").val("");
	    			}
	    		}
	    	});			
		},
		
		
		step2:function(){
			
			$('#reg1Div').addClass('fn-hide');
	    	$('#reg2Div').addClass('fn-hide');
		    $('#reg3Div').removeClass('fn-hide');
			$('#status #js_step1').removeClass('active');
		    $('#status #js_step2').removeClass('active');
		    $('#status #js_step3').addClass('active');	    			
		},
		
		submit : function (){
			
				var entity = {
					id:$("#hiddenId").val(),
					password : $("#password").val(),
				}
		    	
		    	$.ajax({
		    		url :_this.urlItems.submitURL,
		    		data : entity,
		    		dataType : "json",
		    		type : 'post',
		    		success : function(data) {				
		    			if(data.code==_this.completeSuccessCode){ 
		    				//window.webDialogUtil.alert("提示",data.msg);	
		    				//$('#regFrom3')[0].reset();		    				
		    				$('#reg1Div').addClass('fn-hide');
		    		    	$('#reg2Div').addClass('fn-hide');
		    			    $('#reg3Div').addClass('fn-hide');
		    			    $('#reg4Div').removeClass('fn-hide');
		    				$('#status #js_step1').removeClass('active');
		    			    $('#status #js_step2').removeClass('active');
		    			    $('#status #js_step3').addClass('active');	  
		    			}else{
		    				window.webDialogUtil.alert("提示",data.msg);
		    				$('#reg1Div').removeClass('fn-hide');
							$('#reg2Div').addClass('fn-hide');
							$('#reg3Div').addClass('fn-hide');
							$('#status #js_step1').addClass('active');
							$('#status #js_step2').removeClass('active');
							$('#status #js_step3').removeClass('active');
		    			}
		    		}
		    	});	
			
		},
},

$(function(){	
	//绑定回车事件
	$("#userPassword2").keydown(function(event) {		
		var key = event.keyCode || event.which;
		if (key == 13 || (key == 10 && $.browser.msie && $.browser.version <= 6.0)) {
			$("#loginBtn2").click();
		}
    });
	
	passwdCallbackCtrl.passwdCallback.event();	
	//登录
	passwdCallbackCtrl.passwdCallback.login();
	
});